package cn.zf.spring5.test;

import cn.zf.spring5.config.TxConfig;
import cn.zf.spring5.dao.UserDao;
import cn.zf.spring5.dao.UserDaoImpl;
import cn.zf.spring5.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.GenericApplicationContext;

public class testaccount {
    @Test
    public void testAccout(){
        ClassPathXmlApplicationContext context =
                new ClassPathXmlApplicationContext("bean1.xml");
        UserService userservice = context.getBean("userService", UserService.class);
        userservice.accountMoney();
    }

    @Test
    public void testAccout1(){
        ClassPathXmlApplicationContext context =
                new ClassPathXmlApplicationContext("bean2.xml");
        UserService userservice = context.getBean("userS" +
                "ervice", UserService.class);
        userservice.accountMoney();
    }

    @Test
    public void testAccout2(){
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TxConfig.class);
        UserService userservice = context.getBean("userS" +
                "ervice", UserService.class);
        userservice.accountMoney();
    }

    //spring5新功能
    @Test
    public void testGenericApplicationContext(){
        //1 创建GenericApplicationContext对象
        GenericApplicationContext context = new GenericApplicationContext();

        //2 调用context方法 对象注册
        context.refresh();;
        context.registerBean(User.class, () -> new User());

        //3 获取在spring中注册的对象
        User user = (User)context.getBean("cn.zf.spring5.test.User");
        System.out.println(user);
    }
}
